package net.stogi.dsper.api.model;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Version;

@Entity
@Table(name = "user_profiles")
public class UserProfile implements Serializable {

	private static final long serialVersionUID = 830551093221727844L;

	@JoinTable(name = "user_profiles_map_data_sources", joinColumns = { @JoinColumn(name = "user_profile_id") }, inverseJoinColumns = { @JoinColumn(name = "data_source_id") })
	@ManyToMany
	private Set<DataSource> dataSources;

	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Id
	private Long id;

	private String name;

	@JoinTable(name = "user_profiles_map_tags", joinColumns = { @JoinColumn(name = "user_profile_id") }, inverseJoinColumns = { @JoinColumn(name = "tag_id") })
	@ManyToMany
	private Set<Tag> tags;

	@JoinColumn(name = "user_id")
	@ManyToOne
	private User user;

	@Version
	private int version;

	public UserProfile() {
		dataSources = new HashSet<DataSource>();
		tags = new HashSet<Tag>();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof UserProfile)) {
			return false;
		}
		UserProfile other = (UserProfile) obj;
		// Id.
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		}
		else if (!id.equals(other.id)) {
			return false;
		}
		// Name.
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		}
		else if (!name.equals(other.name)) {
			return false;
		}

		return true;
	}

	/**
	 * @return the dataSources
	 */
	public Set<DataSource> getDataSources() {
		return dataSources;
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the tags
	 */
	public Set<Tag> getTags() {
		return tags;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @return the version
	 */
	public int getVersion() {
		return version;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/**
	 * @param dataSources
	 *          the dataSources to set
	 */
	public void setDataSources(Set<DataSource> dataSources) {
		this.dataSources = dataSources;
	}

	/**
	 * @param id
	 *          the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @param name
	 *          the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param tags
	 *          the tags to set
	 */
	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}

	/**
	 * @param user
	 *          the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @param version
	 *          the version to set
	 */
	public void setVersion(int version) {
		this.version = version;
	}
}
